/*******************************************************************************
																				
	DESCRIPTION: 	This do file creates the dataset with wealth variables.
	
*******************************************************************************/

clear all
global id_code 001_4

********************************************************************************
* A1: Wealth data - combine all years 
********************************************************************************

use "${wealthData}/WealthR_1999.dta", clear
	
keep LopNr_PersonNr Ar FNETTMV FKUBANK FSKULMV FREALMV

forvalues year = 2000/2007{

	append using "${wealthData}/WealthR_`year'.dta", keep(LopNr_PersonNr Ar FNETTMV FKUBANK FSKULMV FREALMV)

}

duplicates drop LopNr_PersonNr Ar, force

********************************************************************************
* A2: Wealth data - clean and rename variables
********************************************************************************

* Rename variables 
rename Ar year
destring year, replace 
rename FNETTMV NetWealth
rename FKUBANK BankAccount
rename FSKULMV Liabilities
rename FREALMV RealEstate
	
* Create dummies when value of the vaiable is zero
gen NetWealth0 = (NetWealth==0)
gen BankAccount0 = (BankAccount==0)
gen Liabilities0 = (Liabilities==0)
gen RealEstate0 = (RealEstate==0)

save "${data_intermediate}/${id_code}_Wealth_clean.dta", replace
use "${data_intermediate}/${id_code}_Wealth_clean.dta", clear

/* Create observations for all years for each person in Wealth dataset 
to ensure that we don't unnecessarily drop observations when taking a lag 
(i.e., when the observation appears only in year before unemployment but not in the year of unemployment) */
preserve
use "${data}/001_1_UnemploymentSpells.dta", clear
keep Lop*
duplicates drop
expand 9
bysort Lop*: gen year = _n + 1998

tempfile temp
save `temp'
restore

merge 1:1 LopNr_PersonNr year using `temp'
drop if _merge == 1
drop _merge

save "${data_intermediate}/${id_code}_Wealth_clean.dta", replace


* Create lags
xtset LopNr_PersonNr year 
	
foreach var in NetWealth BankAccount Liabilities RealEstate NetWealth0 BankAccount0 Liabilities0 RealEstate0 {
	
	gen L_`var' = L.`var'
		
}

drop NetWealth BankAccount Liabilities RealEstate NetWealth0 BankAccount0 Liabilities0 RealEstate0
	
compress

save "${data_intermediate}/${id_code}_Wealth_clean.dta", replace
	